package com.course.servlet;

import com.course.entity.Teacher;
import com.course.service.TeacherService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/laoshi_login")
public class LaoshiLoginServlet extends HttpServlet {
    private final TeacherService teacherService = new TeacherService();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        String teacherNumber = request.getParameter("teacherNumber");
        String password = request.getParameter("password");
        String email = request.getParameter("email");

        Teacher teacher = teacherService.loginByTeacherNumberPasswordAndEmail(teacherNumber, password, email);

        if (teacher != null) {
            HttpSession session = request.getSession();
            session.setAttribute("teacher", teacher);
            // 登录成功后重定向到老师主页或其他指定页面
            response.sendRedirect(request.getContextPath() + "/laoshi_index.jsp"); 
        } else {
            request.setAttribute("errorMessage", "工号、密码或邮箱不正确");
            request.getRequestDispatcher("/laoshi_login.jsp").forward(request, response);
        }
    }
} 